package com.hualan.sfzy.controller;

import com.hualan.sfzy.entity.ArticalClassification;
import com.hualan.sfzy.entity.FmClassification;
import com.hualan.sfzy.service.ArticalClassificationService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/articalClassification")
public class ArticalClassificationController {
    @Autowired
    private ArticalClassificationService articalClassificationService;
    @GetMapping("/all")
    public Result findAll(){
        List<ArticalClassification> list =  articalClassificationService.findAll();
        if (!list.isEmpty()){
            return Result.success(list);
        }
        return Result.error("服务器端错误，查询失败");
    }

    @GetMapping
    public Result fineById(Integer id) {
        ArticalClassification articalClassification = articalClassificationService.findById(id);
        if (articalClassification == null) {
            return Result.error("查询成功");
        }
        return Result.success(articalClassification);
    }

    @GetMapping("/page")
    public Result findPage(ArticalClassification articalClassification, Integer pageNum, Integer pageSize){
        Map<String,Object> map = articalClassificationService.findPage(pageNum,pageSize,articalClassification);
        return Result.success(map);
    }

    @DeleteMapping("/batch")
    public Result removeBatch(@RequestParam Integer[] ids){
        int count = articalClassificationService.removeBatch(ids);
        if(count > 0){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @DeleteMapping()
    public Result remove(@RequestParam Integer id){
        int count = articalClassificationService.remove(id);
        if(count > 0){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @PostMapping("/add-selective")
    public Result addSelective(@RequestBody ArticalClassification articalClassification){
        int count = articalClassificationService.addSelective(articalClassification);
        if(count > 0){
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @PutMapping
    public Result editSelective(@RequestBody ArticalClassification articalClassification){
        int count = articalClassificationService.edit(articalClassification);
        if(count > 0){
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }
}
